<?php
namespace app\interfaces\controller;

use think\Db;
use think\Request;
use think\Exception;

class MemberCollect
{

    /*
     * 用户端 - 收藏商品 / 店铺
     * @param  string $id 商品 / 店铺ID
     * @param  string $user_id 会员ID
     * @param  string $type_id 分类ID
     */
    public function collect_create()
    {

        if (!Request::instance()->isPost()) return ['code' => 400, 'message' => '请求方式错误'];

        // 获取参数
        $data = Request::instance()->param();

        if (empty($data['id']) || empty($data['user_id']) || empty($data['type_id'])) throw new Exception('not Empty');

        // 启动事务
        Db::startTrans();
        try {

            $find = Db::name('member_collect')->where(['member_id' => $data['user_id'], 'type_id' => $data['type_id'], 'collect_id' => $data['id']])->value('id');

            if (!empty($find)) return ['code' => 402, 'message' => '您已经收藏,请勿重复收藏'];

            if ($data['type_id'] == '2') {

                if ($data['id'] == $data['user_id']) return ['code' => 402, 'message' => '自己不能收藏自己哦'];

            } else {

                $goods_id = Db::name('shop_goods')->where(['id' => $data['id']])->value('member_id');

                if ($data['user_id'] == $goods_id) return ['code' => 402, 'message' => '自己不能收藏自己商品哦'];

            }

            // 存入收藏
            Db::name('member_collect')->insert(['member_id' => $data['user_id'], 'type_id' => $data['type_id'], 'collect_id' => $data['id'], 'create_time' => time()]);

            // 增长收藏量
            if ($data['type_id'] == 2) {

                Db::name('shop')->where(['member_id' => $data['id']])->setInc('collect_number');

            } else {

                Db::name('shop_goods')->where(['id' => $data['id']])->setInc('collect');

            }

            // 提交事务
            Db::commit();

            return ['code' => 200, 'message' => '成功'];

        } catch (\Exception $e) {

            // 回滚事务
            Db::rollback();

            return ['code' => 401, 'message' => '网络繁忙'];
        }
    }

    /*
     * 用户端 - 收藏商品 / 店铺列表
     * @param  string $id 商品 / 店铺ID
     * @param  string $user_id 会员ID
     * @param  string $type_id 分类ID
     */
    public function collect_list()
    {

        // 获取参数
        $data = Request::instance()->param();

        if (empty($data['user_id']) || empty($data['type_id'])) throw new Exception('not Empty');

        if ($data['type_id'] == '1') {
            $result = Db::view('member_collect', 'id,collect_id,member_id,type_id')
                ->view('shop_goods', 'id as list_id,title,thumb_img,price,sale_number,rebate_percentage', 'shop_goods.id = member_collect.collect_id')
                ->where(['member_id' => $data['user_id'], 'type_id' => '1'])
                ->order('id','desc')
                ->paginate(8);
        } else {
            $result = Db::view('member_collect', 'id,collect_id,member_id,type_id')
                ->view('shop', 'member_id as list_id,title,logo,collect_number,type', 'shop.member_id = member_collect.collect_id')
                ->where(['member_collect.member_id' => $data['user_id'], 'type_id' => '2'])
                ->order('id','desc')
                ->paginate(8);
        }

        return ['code' => 200, 'message' => '成功', 'data' => $result];
    }

    /*
     * 用户端 - 取消收藏商品 / 店铺列表
     * @param  string $id 商品 / 店铺ID
     */
    public function collect_del()
    {

        $id = Request::instance()->param('id');
        $user_id = Request::instance()->param('user_id');

        if (empty($id) || empty($user_id)) throw new Exception('not Empty');

        // 启动事务
        Db::startTrans();
        try {

            $data = Db::name('member_collect')->where(['id' => ['in', $id], 'member_id' => $user_id])->field('collect_id,type_id')->select();

            foreach ($data as $value) {

                // 减少收藏量
                if ($value['type_id'] == 2) {

                    Db::name('shop')->where(['member_id' => $value['collect_id']])->setDec('collect_number');

                } else {

                    Db::name('shop_goods')->where(['id' => $value['collect_id']])->setDec('collect');

                }

            }

            // 取消收藏
            $result = Db::name('member_collect')->where(['id' => ['in', $id], 'member_id' => $user_id])->delete();

            if (empty($result)) return ['code' => 402, 'message' => '已取消该收藏,请勿重复取消'];

            // 提交事务
            Db::commit();

            return ['code' => 200, 'message' => '成功'];

        } catch (\Exception $e) {

            // 回滚事务
            Db::rollback();

            return ['code' => 401, 'message' => '网络繁忙'];
        }

    }
}
